////////////////////////////////////////////////////////////////////////////////
//
//  FLEX USER GROUP HOKURIKU
//  Copyright 2009 Flex User Group Hokuriku
//  All Rights Reserved.
// 
//  NOTICE: Flex User Group Hokuriku permits you to use, 
//  modify, and distribute this file 
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package net.fxug.hokuriku.chocolat.logging
{
    import flash.errors.IOError;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;

    import net.fxug.hokuriku.chocolat.logging.constants.LoggingConstants;

    /**
     * ファイルにログを書き込むためのクラス
     *
     * @see Define/LoggingDefine.as
     *
     * @author mac
     */
    public class Logging
    {
        private static var isInit:Boolean = false;

        /**
         * デフォルトコンストラクタ
         */
        public function Logging()
        {
        }

        /**
         * 書き込み
         *
         * @param message
         *
         * @see _init()
         * @see _write()
         */
        public function write(message:String):void
        {
            // 初期化確認前はディレクトリファイルがあるか確認
            if (!Logging.isInit)
            {
                this._init();

                Logging.isInit = true;
            }
            this._write(message);
        }

        /**
         * 初期化
         *
         * @throws IOError
         * @throws Error
         */
        private function _init():void
        {
            var dir:File = File.applicationStorageDirectory.resolvePath(LoggingConstants.LOGGING_DIR);

            if (!dir.exists)
            {
                try
                {
                    dir.createDirectory();
                }
                catch (e:IOError)
                {
                    throw new IOError("[IOError] Logging::_init Directory Create Failed " + e.message);
                }
                catch (e:Error)
                {
                    throw new Error("[Error] Logging::_init Directory Create Failed " + e.message);
                }
            }
            this._write("File Create");
        }

        /**
         * 書き込み処理実装
         *
         * @param message
         * @throws IOError
         * @throws Error
         */
        private function _write(message:String):void
        {
            var file:File = File.applicationStorageDirectory.resolvePath(LoggingConstants.LOGGING_DIR + File.separator + LoggingConstants.ERROR_LOG_FILE_NAME);

            try
            {
                var file_stream:FileStream = new FileStream();
                file_stream.open(file, FileMode.WRITE);
                file_stream.writeUTFBytes("[" + new Date + "] " + message + File.lineEnding);
                file_stream.close();
            }
            catch (e:IOError)
            {
                throw new IOError("[IOError] Logging::_write FileStream Write Failed " + e.message);
            }
            catch (e:Error)
            {
                throw new Error("[Error] Logging::_write FileStream Write Failed " + e.message);
            }
        }
    }
}